package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.JobCodeHigh;
import com.example.demo.entity.result.NameValue;
import com.example.demo.entity.result.RatioNum;
import com.example.demo.mapper.JobCodeHighMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 ** Create by Wedream on 2021/8/5
 */
@RestController
@RequestMapping("/jobCodeHigh")
public class JobCodeHighController {

    @Resource
    JobCodeHighMapper jobCodeHighMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody JobCodeHigh jobCodeHigh) {
        jobCodeHighMapper.insert(jobCodeHigh);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        jobCodeHighMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "200") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<JobCodeHigh> wrapper = Wrappers.<JobCodeHigh>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(JobCodeHigh::getTitle,search);
        }
        Page<JobCodeHigh> jobCodeHighPage = jobCodeHighMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        List<JobCodeHigh> records = jobCodeHighPage.getRecords(); //查询的（200条）记录

        List<RatioNum> rn160 = new ArrayList<>();
//        System.out.println(rn160);
        Map<String,Integer> mp = new HashMap(); // key:比例->value:该通过比例的数量
        for(int i=10;i<=100;i+=10) {
            StringBuilder key = new StringBuilder(i+"%");
            mp.put(key.toString(),0);
        }

//        for(Map.Entry<String, Integer> entry : mp.entrySet()){
//             System.out.println("foreachEntry : key :" + entry.getKey() + "---> value :"+entry.getValue());
////            nv100.add(new NameValue(entry.getKey(),entry.getValue()));
//        }

        for (JobCodeHigh job:records) {
            Double ratio=job.getPassRatio();
            for(Map.Entry<String, Integer> entry : mp.entrySet()){
//                System.out.println("foreachEntry : key :" + entry.getKey() + "---> value :"+entry.getValue());
                    String s= entry.getKey();
                    String subStr = s.substring(0,s.length()-1); // 去掉百分号
                    int toNum = Integer.parseInt(subStr); //转成整数
                    if(ratio>=toNum-10 && ratio<toNum) {  //对应的map数量+1
                        mp.put(toNum+"%",mp.get(toNum+"%")+1);
                        break;
                    }
            }
        }

        for(Map.Entry<String, Integer> entry : mp.entrySet()){
            rn160.add(new RatioNum(entry.getKey(),entry.getValue()));
        }

        Page<RatioNum> ratioNumPage = new Page();  // 创建RatioNum类型的Page,前端需要该类型的数据
        ratioNumPage.setRecords(rn160);
        ratioNumPage.setTotal(jobCodeHighPage.getTotal());
        ratioNumPage.setSize(jobCodeHighPage.getSize());
        ratioNumPage.setCurrent(jobCodeHighPage.getCurrent());
        ratioNumPage.setOrders(jobCodeHighPage.getOrders());
        ratioNumPage.setPages(jobCodeHighPage.getPages());

        return Result.success(ratioNumPage);
    }


    @PutMapping //改
    public Result<?> update(@RequestBody JobCodeHigh jobCodeHigh) {
        jobCodeHighMapper.updateById(jobCodeHigh);
        return Result.success();
    }
}
